package com.example.yuejian23.yuejian_23.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yuejian23.yuejian_23.Adapter.AdvAdapter;
import com.example.yuejian23.yuejian_23.Adapter.BisaiAdapter;
import com.example.yuejian23.yuejian_23.Adapter.DashiAdapter;
import com.example.yuejian23.yuejian_23.Adapter.HuodongAdapter;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.BisaiBean;
import com.example.yuejian23.yuejian_23.javaBean.DashiBean;
import com.example.yuejian23.yuejian_23.javaBean.HuodongBean;
import com.example.yuejian23.yuejian_23.service.HomeService;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dong on 2017/10/10.
 */

public class HomeFragment extends Fragment
{
    RecyclerView recyclerView;
    RollPagerView mlunbo;
    LinearLayout huodong,dashi,bisai;
    View view = null;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.tab03,container,false);

        initview();
        setadv();
        showData();
        return view;
    }

    private void initview() {
        recyclerView =(RecyclerView) view.findViewById(R.id.home);
        recyclerView.setLayoutManager(new LinearLayoutManager(HomeFragment.this.getContext()));
        mlunbo = (RollPagerView) view.findViewById(R.id.roll_view_pager);
        huodong = (LinearLayout) view.findViewById(R.id.huodong);
        dashi = (LinearLayout) view.findViewById(R.id.dashi);
        bisai = (LinearLayout) view.findViewById(R.id.bisai);
        huodong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showData();
            }
        });
        dashi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dashi();
            }
        });
        bisai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bisai();
            }
        });
    }

    private void setadv() {
        mlunbo.setPlayDelay(3000);
        mlunbo.setAnimationDurtion(500);
        mlunbo.setAdapter(new AdvAdapter());
        mlunbo.setHintView(new ColorPointHintView(getActivity(), Color.YELLOW, Color.WHITE));
    }

    private void showData()
    {

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        HomeService homeService=retrofit.create(HomeService.class);

        Call<List<HuodongBean>> call=homeService.getList();

        call.enqueue(new Callback<List<HuodongBean>>() {
            @Override
            public void onResponse(Call<List<HuodongBean>> call, Response<List<HuodongBean>> response) {
                List<HuodongBean> home=response.body();

                recyclerView.setAdapter(new HuodongAdapter(getActivity(),home));

            }

            @Override
            public void onFailure(Call<List<HuodongBean>> call, Throwable t) {

            }
        });

    }

    private void dashi()
    {

        Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

        HomeService homeService=retrofit.create(HomeService.class);

        Call<List<DashiBean>> call=homeService.getLists();

        call.enqueue(new Callback<List<DashiBean>>() {
            @Override
            public void onResponse(Call<List<DashiBean>> call, Response<List<DashiBean>> response) {
                List<DashiBean> home=response.body();

                recyclerView.setAdapter(new DashiAdapter(getActivity(),home));

            }

            @Override
            public void onFailure(Call<List<DashiBean>> call, Throwable t) {

            }
        });

    }

    private void bisai()
    {

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        HomeService homeService=retrofit.create(HomeService.class);

        Call<List<BisaiBean>> call=homeService.getsList();

        call.enqueue(new Callback<List<BisaiBean>>() {
            @Override
            public void onResponse(Call<List<BisaiBean>> call, Response<List<BisaiBean>> response) {
                List<BisaiBean> home=response.body();

                recyclerView.setAdapter(new BisaiAdapter(getActivity(),home));

            }

            @Override
            public void onFailure(Call<List<BisaiBean>> call, Throwable t) {

            }
        });

    }
}
